<?php
    require "../init.php";

    if(empty($_SESSION['home'])){
        redirect("请先登录",3,ROOT_URL . "login.php");
        die;
    }

    //获取get传进来的动作
    $act = $_REQUEST["act"];
    //获取登录用户的id

    $id = $_SESSION['home']['id'];

    switch ($act){
        //点赞
        case 'zan' :
            $goods_id = $_GET['goods_id'];
            //查询该用户已为该商品点过赞没有
            $sql = "select id from " . PRE . "user_zan where user_id = '$id' and  goods_id = '$goods_id'";
            $res = getRow($link, $sql);
            if(!$res){
                //如果没有,就执行插入操作
                $sql = 'insert into '. PRE . "user_zan (user_id,goods_id) values ('$id','$goods_id')";
                $zan_id = execute($link, $sql);//返回插入数据的自增id

                if($zan_id){
                    //点赞成功后,把商品表中的该商品的点赞数目更新
                    $sql = "update ".PRE ."goods set zan = zan+1 where id = '$goods_id'";
                    $res = execute($link, $sql);
                    if($res){
                        //如果商品的赞的数目更新成功,就返回首页
                        redirect('点赞成功,谢谢您对该商品的喜爱',1,ROOT_URL.'./index.php');
                        die;
                    }else{
                        //如果商品的赞的数目更新失败,就把上面刚插入的的数据删除
                        $sql = "delete from " . PRE ."user_zan where id = '$zan_id'";
                        $res = execute($link,$sql);
                        redirect('点赞失败',1,ROOT_URL.'./index.php');
                        die;
                    }

                }else{
                    redirect('点赞失败',1,ROOT_URL.'./index.php');
                    die;
                }
            }else{
                //如果点过赞了就返回
                redirect('您已为该商品点过赞,谢谢您对该商品的喜爱',1,ROOT_URL.'./index.php');
                die;
            }
            break;

        //立即购买
        case 'buy' :
            //接受商品id和要购买的商品数量和该商品的库存量
            $goods_id = $_POST['goods_id'];
            $qty = $_POST['qty'];
            $stock = $_POST['stock'];
            //如果要购买的数量小于1或大于库存量,就返回商品详情页,并提示
            if(($qty < 1) || empty($qty)){
                redirect("商品数量不对",1);die;
            }elseif($qty > $stock){
                redirect("对不起,没有那么多商品",1);die;
            }
            header("location:".ROOT_URL."/buy_goods.php?goods_id=$goods_id&qty=$qty");


            break;

        //加入购物车
        case 'add_cart' :
            //接受商品id和要购买的商品数量和该商品的库存量
            $goods_id = $_POST['goods_id'];
            $qty = $_POST['qty'];//要购买的商品数量
            $stock = $_POST['stock'];//库存量
            if($stock < $qty){
                redirect("库存量不足");die;
            }
            //判断购物车中是否已经有该商品
            $sql = "select id from ".PRE ."cart where goods_id = '$goods_id' and user_id = '$id' limit 1";
            $res = getRow($link, $sql);
            if(!$res){
                //如果没有就增加到购物车
                //先查询该商品的一些信息 商品名字,价格
                $sql = "select gname,price from " . PRE ."goods where id = '$goods_id'";
                $goods_info = getRow($link, $sql);
                if($goods_info){
                    //把商品添加到购物车
                    $sql = "insert into ".PRE."cart (user_id,goods_id,goods_name,goods_price,goods_number,create_time) values ('$id','$goods_id','".$goods_info['gname']."','".$goods_info['price']."','$qty','".DATE."')";
                    $res = execute($link, $sql);
                    if($res){
                        redirect('成功把该物品添加到购物车',1,ROOT_URL."index.php");
                        die;
                    }else{
                        redirect('添加购物车失败',1);
                        die;
                    }
                }
            }else{
                //如果购物车中有过该商品
                $sql = "update ".PRE."cart set goods_number = goods_number +$qty where goods_id = '$goods_id'";
                $res = execute($link, $sql);
                if($res){
                    redirect('成功把该物品添加到购物车',1,ROOT_URL."index.php");
                    die;
                }else{
                    redirect('添加购物车失败',1);
                    die;
                }
            }
            break;

        //新增地址
        case 'add_address' :
            //接收地址信息
            $address = $_POST['address'];
            if(empty($address)){
                redirect("地址不能为空");die;
            }
            //写sql语句把地址插入到数据库
            $sql = "insert into " . PRE . "user_address (user_id,address,create_time) values ('$id','$address','".DATE."')";
            $res = execute($link, $sql);
            if($res){
                redirect('新增地址成功',1,ROOT_URL."index.php");
                die;
            }else{
                redirect('地址添加失败',1);
                die;
            }
            break;

        //设为默认地址
        case 'set_default_address' :
            //接收地址id
            $address_id = $_GET['id'];
            //先把该用户的其他地址设为不默认
            $sql = "update ". PRE ."user_address set default_a = 0 where user_id='$id'";
            $res = execute($link, $sql);
            if(!$res){
                redirect("设置默认地址失败",1);
                die;
            }
            $sql = "update ". PRE ."user_address set default_a = 1 where user_id='$id' and id='$address_id'";
            $res = execute($link, $sql);
            if(!$res){
                redirect("设置默认地址失败",1);
                die;
            }else{
                redirect("设置默认地址成功",1);
                die;
            }
            break;

        //删除购物车中的商品
        case 'delete_cart' :
            //先获取要删除商品的id
            $goods_id = $_GET['goods_id'];
            if(!empty($goods_id)){
                $sql = "delete from " .PRE ."cart where goods_id='$goods_id' and user_id='$id'";
                $res = execute($link, $sql);
                if($res){
                    redirect('成功删除',1);
                    die;
                }else{
                    redirect('删除失败',1);
                    die;
                }
            }

            break;

        //付款
        case 'pay' :
            //接受订单编号
            $order_id = $_POST['order_id'];
            //把该订单的付款状态pay_status和订单状态shiping改为已付款和已确认
            $sql = "update ".PRE."order set pay_status=1,shipping_status=1 where id = '$order_id'";
            $res = execute($link,$sql);
            redirect("付款成功",3,ROOT_URL."index.php");
            break;

        //删除订单
        case 'delete_order':
            //获取要删除的订单id
            $order_id = $_GET['order_id'];
            //先删除该订单下的商品
            $sql = "select id from ".PRE."ordergoods where order_id='$order_id'";
            $order_goods_list = query($link, $sql);
            if($order_goods_list){
                foreach($order_goods_list as $value){
                    $order_goods_id = $value['id'];
                    $sql = "delete from ".PRE."ordergoods where id='$order_goods_id'";
                    execute($link, $sql);
                }
                $sql = "delete from ".PRE."order where id='$order_id' and user_id='$id'";
                $res = execute($link, $sql);
                if($res){
                    redirect('成功删除订单');die;
                }else{
                    redirect('删除订单失败');die;

                }
            }



            break;

        //确认收货
        case 'confirm' :
            //获得订单id
            $order_id = $_GET['order_id'];
            //把该订单的order_status改为已完成
            $sql = "update ".PRE."order set order_status=2 where id = '$order_id'";
            $res = execute($link,$sql);
            if($res){
                //把销量加上
                $sql = "select goods_id,qty from ".PRE."ordergoods where order_id = '$order_id' ";
                $goods_list = query($link, $sql);
                //把每一件商品的库存量减去发出去的该商品的库存量
                foreach ($goods_list as $item) {
                    $goods_id = $item['goods_id'];
                    $qty = $item['qty'];
                    $sql = "update ".PRE."goods set sale = sale + $qty where id = '$goods_id'";
                    execute($link,$sql);
                }
                redirect("确认收货成功",1);
            }

            break;

        //更改个人信息
        case 'update_userinfo' :
            //查看表单元素是否有空的
            foreach($_POST as $item){
                if(empty($item)){
                    redirect("表单元素不能为空", 3 );
                    die;
                }
            }

            $name = $_POST['name'];
            $id = $_POST['id'];
            $tel = $_POST['tel'];
            $email = $_POST['email'];

            //用户名规范验证
            $regex_name = '/^[a-zA-Z]\w{3,14}$/';
            if(!preg_match($regex_name, $name)){
                redirect("用户名格式不正确");
                die;
            }

            //验证手机号
            $regex_tel = '/^1\d{10}$/';
            if(!preg_match($regex_tel, $tel)){
                redirect("手机号格式不正确");
                die;
            }

            //验证邮箱
            $regex_email = '/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+$/';
            if(!preg_match($regex_email, $email)){
                redirect("邮箱格式不正确");
                die;
            }

            //准备sql语句,查询该用户是否已存在
            $sql = "select id from ". PRE . "user where name = '" . $name . "' and id != $id limit 1";

            // 执行sql语句
            $result = getRow($link, $sql);


            //判断该用户是否已存在
            if($result){
                redirect("该名称已存在");
                die;
            }

            //准备sql更改数据库
            $sql = "update ". PRE . "user set name = '" . $name ."', tel = '" . $tel ."',
        email = '" . $email ."' where id = '" . $id ."'";
            //执行sql语句
            $result = execute($link, $sql);
            if($result){
                $_SESSION['home']['name'] = $name;
                redirect("个人信息修改成功",3,ROOT_URL . "index.php");
                die;
            }else{
                redirect("个人信息修改失败");
                die;
            }
            break;

        //更改个人密码
        case 'update_pwd' :
            //查看表单元素是否有空的
            foreach($_POST as $item){
                if(empty($item)){
                    redirect("表单元素不能为空", 3 );
                    die;
                }
            }
            //或去新旧密码
            $old_pwd = md5($_POST['old_pwd']);
            $new_pwd = md5($_POST['new_pwd']);
            $renew_pwd = md5($_POST['renew_pwd']);
            if($new_pwd != $renew_pwd){
                redirect("两次新密码输入不一致");
            }
            $sql = "select id,pwd from ".PRE."user where id='$id'";
            $user_info = getRow($link,$sql);
            if($user_info['pwd'] != $old_pwd){
                redirect("旧密码输入不正确");
            }
            $sql = "update ".PRE."user set pwd = '$new_pwd' where id='$id'";
            $res = execute($link, $sql);
            if($res){
                unset($_SESSION['home']);
                redirect("修改密码成功,请重新登录",3,ROOT_URL.'login.php');
                die;
            }else{
                redirect("修改密码成功失败");
                die;
            }
            break;

        //用户评论
        case 'evaluate' :
//            echo "<pre>";
//            print_r($_POST);
//            print_r($_FILES);
            //先判断评论内容是否为空
            if(empty($_POST['evaluate'])){
                redirect("评论内容不能为空");die;
            }
            $evaluate = $_POST['evaluate'];
            //判断图片是否上传
            if(!empty($_FILES['myfile']['name'])){
                // 上传图片保存路径
                $save_dir = ROOT_PATH.'upload_evaluate_image';
                //开始上传图片
                $file_name = uploadFile('myfile','10485760',array("image"),$save_dir);
                if(!$file_name){
                    admin_redirect("上传图片失败");die;
                }

                //获取图片的名称
                $basename = basename($file_name);
                //开始缩放图片，如果有一张缩放失败，就删除所以缩放图片和刚上传的图片
                if(!zoomImg($file_name,350,350) || !zoomImg($file_name,250,250) || !zoomImg($file_name,150,150) || !zoomImg($file_name,50,50)){
                    //如果有一张图片缩放失败,就删除所有刚才缩放成功的
                    @unlink($file_name);
                    @unlink(dirname($file_name)."/350_".$basename);
                    @unlink(dirname($file_name)."/250_".$basename);
                    @unlink(dirname($file_name)."/150_".$basename);
                    @unlink(dirname($file_name)."/50_".$basename);
                    redirect("图片缩放失败");
                    die;
                }
                $goods_id = $_POST['goods_id'];
                //准备sql语句,向数据库中添加评论
                $sql = "insert into ".PRE."evaluate (user_id,goods_id,content,image_name) values ('$id','$goods_id','$evaluate','$basename')";
                $evaluate_id = execute($link, $sql);
                if(!$evaluate_id){
                    //如果评论添加失败
                    @unlink($file_name);
                    @unlink(dirname($file_name)."/350_".$basename);
                    @unlink(dirname($file_name)."/250_".$basename);
                    @unlink(dirname($file_name)."/150_".$basename);
                    @unlink(dirname($file_name)."/50_".$basename);
                    redirect("评论失败");
                    die;
                }
                //写入图片信息
                $sql = "insert into ".PRE."evaluate_image (evaluate_id,image_name) values ('$evaluate_id','$basename')";
                $res = execute($link, $sql);
                if(!$res){
                    //如果写入图片失败,就把刚才的操作都删除
                    $sql = "delete from ".PRE."evaluate where id = '$evaluate_id'";
                    execute($link,$sql);
                    @unlink($file_name);
                    @unlink(dirname($file_name)."/350_".$basename);
                    @unlink(dirname($file_name)."/250_".$basename);
                    @unlink(dirname($file_name)."/150_".$basename);
                    @unlink(dirname($file_name)."/50_".$basename);
                    redirect("图片写入失败");
                    die;
                }else{
                    redirect("评论成功",3,ROOT_URL.'user_center.php?act=bought');
                    die;
                }
            }else{
                //如果没有图片
                $goods_id = $_POST['goods_id'];
                //准备sql语句,向数据库中添加评论
                $sql = "insert into ".PRE."evaluate (user_id,goods_id,content,image_name) values ('$id','$goods_id','$evaluate','')";
                $evaluate_id = execute($link, $sql);
                if($evaluate_id){
                    //写入图片信息
                    $sql = "insert into ".PRE."evaluate_image (evaluate_id,image_name) values ('$evaluate_id','')";
                    $res = execute($link, $sql);
                    redirect("评论成功",3,ROOT_URL.'user_center.php?act=bought');die;
                }else{
                    redirect("评论失败");die;
                }

            }

            break;

        //修改头像
        case 'user_head' :

            if(empty($_FILES['ming'])){
                redirect('图片不能为空');
            }

            // 上传图片保存路径
            $save_dir = ROOT_PATH.'user_head';
            //开始上传图片
            $file_name = uploadFile('ming','10485760',array("image"),$save_dir);
            if(!$file_name){
                redirect("上传图片失败");die;
            }
            //获取图片的名称
            $basename = basename($file_name);
            if(!zoomImg($file_name,30,30)){
                @unlink($file_name);
                redirect('图片缩放失败');
                die;
            }
            @unlink($file_name);//删掉原图

            //写入图片

            //查新该用户是否已经有头像,如果有,修改成功后,就删除
            $sql = "select id,head_image from ".PRE."user_head where user_id= '$id'";
            $res = getRow($link, $sql);
            if($res){
                $head_id = $res['id'];
                $sql = "update ".PRE."user_head set head_image = '$basename' where id='$head_id' and user_id='$id'";
                $head_id = execute($link,$sql);
            }else{
                $sql = "insert into ".PRE."user_head (user_id,head_image,create_time) values ('$id','$basename','".DATE."')";
                $head_id = execute($link, $sql);
            }
            if(!$head_id){
                @unlink(dirname($file_name)."/30_".$basename);
                redirect('图片写入失败');
                die;
            }else{
                $head_dir = get_img_url(ROOT_PATH.'user_head/',$_SESSION['home']['head_image'],30);
                @unlink($head_dir);//删除原头像
                $_SESSION['home']['head_image'] = $basename;
                redirect('头像设置成功',3,ROOT_URL.'index.php');
            }
            break;

    }

?>